/**
 * 
 */
package cn.zqy.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import cn.zqy.model.Library;

public class LibraryService extends PersistentService
{
	public ArrayList<Library> retrieveAll()
	{
		ArrayList<Library> bookStoreList = new ArrayList<Library>();
		for (Object library : retrieveAll(Library.class, ""))
		{
			bookStoreList.add((Library) library);
		}
		return bookStoreList;
	}

	public Library retrieveByBookNo(String bookNo)
	{
		Library library;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			 library=(Library)pm.getObjectById(Library.class,bookNo);
		}
		finally
		{
			pm.close();
		}
		return library;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Library> retrieveAll(HashMap<String,String> params)
	{
		ArrayList<Library> libraryList = new ArrayList<Library>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String condition="";
		StringBuffer conditionSb=new StringBuffer();
		
		if(params.containsKey("bookStatus"))
		{
			conditionSb.append(" && bookStatus=='"+params.get("bookStatus")+"'");
		}
		if(params.containsKey("bookNo"))
		{
			conditionSb.append(" && bookNo=='"+params.get("bookNo")+"'");
		}
		if(params.containsKey("bookName"))
		{
			conditionSb.append(" && bookName=='"+params.get("bookName")+"'");
		}
		if(params.containsKey("language"))
		{
			conditionSb.append(" && language=='"+params.get("language")+"'");
		}
		if(conditionSb.length()>3)
		{
			condition=conditionSb.substring(3);
		}
		Query query = null;
		if(condition.length()==0)
		{
			query = pm.newQuery(Library.class);
		}
		else
		{
			query = pm.newQuery(Library.class,condition);
		}
		try
		{
			query.setOrdering("bookNo asc");
			for (Library library : (List<Library>) query.execute())
			{
				libraryList.add(library);
			}
		}
		finally
		{
			query.closeAll();
			pm.close();
		}
		return libraryList;
	}
	
	public void delete(String bookNo)
	{
/*		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction trans=pm.currentTransaction();
		//String bookNo=library.getBookNo();		
		try
		{	
			trans.begin();
			Library library=(Library)pm.getObjectById(Library.class,bookNo);
			pm.deletePersistent(library);
			trans.commit();
		}
		catch(Exception e)
		{
			log.severe(e.toString());
		}
		finally
		{
			if(trans.isActive())
			{
				trans.rollback();
			}
			pm.close();
		}*/
		super.delete(Library.class, bookNo);
	}
}
